package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminIssueCreateBo;
import com.cskaoyan.bean.bo.AdminIssueDeletBo;
import com.cskaoyan.bean.bo.AdminIssueListBo;
import com.cskaoyan.bean.bo.AdminIssueUpdateBo;
import com.cskaoyan.bean.po.MarketIssue;
import com.cskaoyan.bean.po.MarketIssueExample;
import com.cskaoyan.bean.vo.AdminOrderShipVo;
import com.cskaoyan.bean.vo.admin_issue_create.AdminIssueCreateDataEntityVo;
import com.cskaoyan.bean.vo.admin_issue_create.AdminIssueCreateVo;
import com.cskaoyan.bean.vo.admin_issue_list.AdminIssueListDataEntityVo;
import com.cskaoyan.bean.vo.admin_issue_list.AdminIssueListListEntityVo;
import com.cskaoyan.bean.vo.admin_issue_list.AdminIssueListVo;
import com.cskaoyan.bean.vo.admin_issue_update.AdminIssueUpdateDataEntityVo;
import com.cskaoyan.bean.vo.admin_issue_update.AdminIssueUpdateVo;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Trellis
 * @since 2022/09/07 15:40
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService {


    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public AdminIssueListVo getListByBo(AdminIssueListBo listBo) {
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());
        MarketIssueExample example = new MarketIssueExample();
        example.setDistinct(true);
        String clause = listBo.getSort() + " " + listBo.getOrder();
        example.setOrderByClause(clause);
        if (listBo.getQuestion() != null) {

            example.createCriteria().andQuestionLike("%"+listBo.getQuestion()+"%");
        }

        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(example);

        ArrayList<AdminIssueListListEntityVo> listEntityVos = new ArrayList<>();
        for (MarketIssue marketIssue : marketIssues) {
            AdminIssueListListEntityVo entityVo = new AdminIssueListListEntityVo(marketIssue.getDeleted(), marketIssue.getQuestion(), marketIssue.getAnswer(),
                    marketIssue.getAddTime(), marketIssue.getUpdateTime(), marketIssue.getId());
            listEntityVos.add(entityVo);
        }
        PageInfo pageInfo = new PageInfo(marketIssues);
        int total = (int) pageInfo.getTotal();
        AdminIssueListDataEntityVo dataEntityVo = new AdminIssueListDataEntityVo(total, pageInfo.getPages(), listBo.getLimit(), listBo.getPage(), listEntityVos);
        AdminIssueListVo issueListVo = new AdminIssueListVo(0, dataEntityVo, "成功");
        return issueListVo;
    }

    @Override
    public AdminIssueUpdateVo updateMessage(AdminIssueUpdateBo updateBo) {
        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setQuestion(updateBo.getQuestion());
        marketIssue.setAnswer(updateBo.getAnswer());
        marketIssue.setId(updateBo.getId());
        //更新数据库中的信息
        int key = marketIssueMapper.updateByPrimaryKeySelective(marketIssue);
        //返回响应信息
        AdminIssueUpdateVo adminIssueUpdateVo = null;
        if (key == 1) {
            MarketIssue marketIssue1 = marketIssueMapper.selectByPrimaryKey(updateBo.getId());
            AdminIssueUpdateDataEntityVo dataEntityVo = new AdminIssueUpdateDataEntityVo(marketIssue1.getDeleted(), marketIssue1.getQuestion(),
                    marketIssue1.getAnswer(), marketIssue1.getAddTime(), marketIssue1.getUpdateTime(), marketIssue1.getId());

            adminIssueUpdateVo = new AdminIssueUpdateVo(0, dataEntityVo, "成功");

        }

        return adminIssueUpdateVo;
    }

    @Override
    public AdminIssueCreateVo createMessage(AdminIssueCreateBo createBo) throws ParseException {
        //获取现在时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String format = dateFormat.format(date);
        //获取数据库中类型的时间
        Date time = dateFormat.parse(format);

        MarketIssue marketIssue = new MarketIssue();
        marketIssue.setId(null);
        marketIssue.setQuestion(createBo.getQuestion());
        marketIssue.setAnswer(createBo.getAnswer());
        marketIssue.setAddTime(time);
        marketIssue.setUpdateTime(time);
        marketIssue.setDeleted(false);
        //向数据库中插入数据
        marketIssueMapper.insert(marketIssue);
        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andAddTimeEqualTo(time);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(example);
        Integer id = marketIssues.get(0).getId();
        AdminIssueCreateDataEntityVo dataEntityVo = new AdminIssueCreateDataEntityVo(createBo.getQuestion(), createBo.getAnswer(), time, time, id);
        AdminIssueCreateVo adminIssueCreateVo = new AdminIssueCreateVo(0, dataEntityVo, "成功");

        return adminIssueCreateVo;
    }

    @Override
    public AdminOrderShipVo deleteByBo(AdminIssueDeletBo deletBo) {
        int key = marketIssueMapper.deleteByPrimaryKey(deletBo.getId());
        AdminOrderShipVo orderShipVo = null;
        if (key == 1) {
            orderShipVo = new AdminOrderShipVo(0, "成功");
        }
        return orderShipVo;
    }
}
